/**
 * 
 */
package de.cbf.cam.dsa.model.persistence;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import de.cbf.cam.dsa.model.Activator;
import de.cbf.cam.dsa.model.base.BaseWeapon;
import de.cbf.cam.dsa.model.entities.ArmorPart;
import de.cbf.cam.dsa.model.entities.items.Item;


/**
 * @author Nebelritter
 *
 */
public class Persister {
		
	private static Persister instance ;
	/**
	 * @uml.property  name="em"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="de.cbf.cam.dsa.model.entities.items.Item"
	 */
	private EntityManager em;

//========================
//		constructors
//========================
	/**
	 * 
	 */
	public Persister() {
		em = HibernateUtil.getEntityManager();
	}
//========================
//	   public methods
//========================
	public static Persister getInstance() {
		if(instance == null){
			instance = new Persister();
		}
		return instance;
	}
	
	public BaseWeapon getBaseWeapon(String weaponType) {
		EntityTransaction tx = em.getTransaction();
		String queryString = "select weapon as BaseWeapon from BaseWeapon where type = Type";
		tx.begin();
		Query query = em.createQuery(queryString);
		query.setParameter("Type",weaponType);
		
		List<?> results = query.getResultList();
		List<BaseWeapon> weapons = new ArrayList<BaseWeapon>();
		for(int i = 0; i < results.size();i++){
			if (results.get(i) instanceof BaseWeapon) {
				BaseWeapon weapon = (BaseWeapon) results.get(i);
				weapons.add(weapon);
			}
		}
		tx.commit();
		if(weapons.size()==1){
			return weapons.get(0);
		}else{
			Activator.logger.error("Query for BaseWeapons returned wrong number of Weapons: "+weapons.size());
			return null;
		}	
	}
	
	public Item getItemByName(String name) {		
		EntityTransaction tx = em.getTransaction();
		String queryString = "select item as Item from Items where name = Name";
		tx.begin();
		Query query = em.createQuery(queryString);
		query.setParameter("Name",name);
		
		List<?> results = query.getResultList();
		List<Item> items = new ArrayList<Item>();
		
		for(int i = 0; i < results.size();i++){
			if (results.get(i) instanceof Item) {
				Item item = (Item) results.get(i);				
				items.add(item);
			}
		}
		tx.commit();
		if(items.size()==1){
			return items.get(0);
		}else{
			Activator.logger.error("Query for Items returned wrong number of Items: "+items.size());
			return null;
		}	
	}
	public ArmorPart getArmorByName(String name) {
		EntityTransaction tx = em.getTransaction();
		String queryString = "select armor as ArmorPart from ArmorPart where name = Name";
		tx.begin();
		Query query = em.createQuery(queryString);
		query.setParameter("Name",name);
		
		List<?> results = query.getResultList();
		List<ArmorPart> armorParts = new ArrayList<ArmorPart>();
		
		for(int i = 0; i < results.size();i++){
			if (results.get(i) instanceof ArmorPart) {
				ArmorPart item = (ArmorPart) results.get(i);				
				armorParts.add(item);
			}
		}
		tx.commit();
		if(armorParts.size()==1){
			return armorParts.get(0);
		}else{
			Activator.logger.error("Query for ArmorParts returned wrong number of ArmorParts: "+armorParts.size());
			return null;
		}	
	}



//========================
//    standard methods
//========================

//========================
// 	   private methods
//========================

//========================
// 	SHORT property access
//========================

//========================
//		getters
//========================

//========================
//		setters
//========================

//========================
//		listeners
//========================

}
